package com.gonsin.i18n.logic.utils;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class AESUtils {

    /**
     * 加密
     */
    @SneakyThrows
    public static String encryptECB(String data, String key, String encoder)  {
        AesInstance instance = AesInstance.getInstance(key.getBytes(encoder), key.getBytes(encoder));
        return instance.encText(data);
    }

    /**
     * 解密
     */
    @SneakyThrows
    public static String decryptECB(String base64Data, String key, String encoder) {
        AesInstance instance = AesInstance.getInstance(key.getBytes(encoder), key.getBytes(encoder));
        return instance.decText(base64Data);
//        try {
//            Cipher cipher = Cipher.getInstance("Aes/ECB/PKCS5Padding");
//            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(encoder), "Aes"));
//            byte[] data = Base64.getDecoder().decode(base64Data);
//            byte[] result = cipher.doFinal(data);
//            return new String(result, encoder);
//        } catch (Exception ex) {
//            ex.printStackTrace();
//            return null;
//        }
    }

}
